package com.star.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.star.constant.MessageConstant;
import com.star.constant.StatusConstant;
import com.star.context.BaseContext;
import com.star.dto.CategoryDTO;
import com.star.dto.CategoryPageQueryDTO;
import com.star.entity.Category;
import com.star.exception.DeletionNotAllowedException;
import com.star.mapper.CategoryMapper;
import com.star.mapper.DishMapper;
import com.star.mapper.SetmealMapper;
import com.star.result.PageResult;
import com.star.service.CategoryService;
import com.star.vo.CategoryVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CategoryServiceImpl implements CategoryService {

    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;

    /**
     * 新增分类
     * @param categoryDTO
     */
    public void addCategory(CategoryDTO categoryDTO) {
        Category category = new Category();
        // 属性拷贝
        BeanUtils.copyProperties(categoryDTO, category);
        // 商家id
        category.setBusinessId(BaseContext.getCurrentId());
        // 设置分类状态 默认状态 1:正常 0:禁用
        category.setStatus(StatusConstant.DISABLE);

        // 公共字段自动注入 create_time,create_user,update_time,update_user

        categoryMapper.insert(category);
    }

    /**
     * 分页查询
     * @param categoryPageQueryDTO
     * @return
     */
    public PageResult pageQuery(CategoryPageQueryDTO categoryPageQueryDTO) {
        // 开启分页查询
        PageHelper.startPage(categoryPageQueryDTO.getPage(), categoryPageQueryDTO.getPageSize());
        Page<CategoryVO> page = categoryMapper.pageQuery(categoryPageQueryDTO);
        return new PageResult(page.getTotal(), page.getResult());
    }

    /**
     * 根据id查询分类
     * @param id 分类id
     * @return
     */
    public Category getById(Long id) {
        return categoryMapper.getById(id);
    }

    /**
     * 更新分类信息
     * @param categoryDTO
     */
    public void update(CategoryDTO categoryDTO) {
        // 属性拷贝
        Category category = new Category();
        BeanUtils.copyProperties(categoryDTO, category);

        // 公共字段自动注入

        categoryMapper.update(category);
    }

    /**
     * 更新分类状态
     * @param status 分类状态
     * @param id 分类id
     */
    public void updateStatus(Integer status, Long id) {
        Category category = categoryMapper.getById(id);
        category.setStatus(status);
        categoryMapper.update(category);
    }

    /**
     * 根据id删除分类
     * @param id 分类id
     */
    public void delete(Long id) {
        Category category = categoryMapper.getById(id);
        if(category.getStatus().equals(StatusConstant.ENABLE)){
            // 启用的分类不删除
            throw new DeletionNotAllowedException(MessageConstant.CATEGORY_ON_SALE);
        }
        // 查询当前分类是否关联菜品，不能删除
        Integer dishCount = dishMapper.countByCategoryId(id);
        if(dishCount > 0) {
            throw new DeletionNotAllowedException(MessageConstant.CATEGORY_BE_RELATED_BY_DISH);
        }
        // 查询当前分类是否关联套餐，不能删除
        Integer mealCount = setmealMapper.countByCategoryId(id);
        if(mealCount > 0) {
            throw new DeletionNotAllowedException(MessageConstant.CATEGORY_BE_RELATED_BY_SETMEAL);
        }
        // 成功删除分类
        categoryMapper.delete(id);
    }

    /**
     * 根据类型查询分类
     * @param type 分类类型
     * @return
     */
    public List<Category> getByType(Long businessId,Integer type) {
        return categoryMapper.getByType(businessId, type);
    }

    /**
     * 商家分类列表查询
     * @param businessId
     * @return
     */
    public List<Category> getList(Long businessId) {
        return categoryMapper.getByBusinessId(businessId);
    }
}
